class UsersController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @user_pages, @users = paginate :users, :per_page => 10
#    @Gusers = []
#    @users.each do |u|
#        @Gusers << ( Goldberg::User.find(u.id))
#    end
  end

  def show
    @user = User.find(params[:id])
    @Guser = Goldberg::User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    goldbergParams = getGoldbergParams
    @goldberg_user = Goldberg::User.new(goldbergParams)
    if @goldberg_user.save
        @goldberg_user.reload
        if params[:user] == nil
            params[:user] = {}
        end
        params[:user][:id] = @goldberg_user[:id]
        @user = User.new(params[:user])
        if @user.save
          flash[:notice] = 'Attorney was successfully created.'
          redirect_to :action => 'list'
        else
          render :action => 'new'
        end
    else
        render :action => 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
    @Guser = Goldberg::User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    @goldberg_user = Goldberg::User.find(params[:id])
    goldbergParams = getGoldbergParams
    if params[:user] == nil
        params[:user] = {}
    end
    if @usser.update_attributes(params[:user])
      @goldberg_user.update_attributes(goldbergParams)
      flash[:notice] = 'Attorney was successfully updated.'
      redirect_to :action => 'show', :id => @user
    else
      render :action => 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def tag_list
    @user = User.find(:first, :conditions => ["users.id = ?", Goldberg.user.id], :include => :tags)
    puts @user.inspect
    @tags = @user.tags
    render :partial => 'tags_cloud' 
  end

  def getGoldbergParams
    goldbergParams = {}
    goldbergParams[:name] = params[:Guser][:username]
    goldbergParams[:confirm_password] = params[:Guser][:password]
    goldbergParams[:clear_password] = params[:Guser][:password]
    goldbergParams[:role_id] = params[:Guser][:role] ? params[:Guser][:role] : 2 
    goldbergParams[:fullname] = params[:Guser][:firstname] + " " + params[:Guser][:lastname]
    goldbergParams[:email] = params[:Guser][:email]
    goldbergParams[:start_path] = "/groups/list"
    return goldbergParams
  end
end
